import Tab from "vux/src/components/tab/tab.vue";
import TabItem from "vux/src/components/tab/tab-item.vue";
import Swiper from "vux/src/components/swiper/swiper.vue";
import SwiperItem from "vux/src/components/swiper/swiper-item.vue";

import { mapGetters, mapActions } from "vuex";
import template from "./taskCenterTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  components: {
    Tab,
    TabItem,
    Swiper,
    SwiperItem,
  },
  data() {
    return {
      taskData: [],
      taskList: [],
      curIndex: 0,
      pullDownRefresh: true,

      signStatus: [], //签到状态数据
      signInFlag: false, //签到弹窗
      signInOKFlag: false, //签到成功弹窗
      signMoneyBal: 0,
      signMoney: 0,
    };
  },
  created() {},
  mounted() {
    this._getTaskData(1);
  },
  computed: {
    ...mapGetters(["accountData", "getSignObj"]),
  },
  methods: {
    ...mapActions(["SET_ACCOUNT_DATA"]),
    toggleRule(index) {
      var obj = {};
      obj = this.taskData[this.curIndex];
      obj.data[index].seen = !obj.data[index].seen;
      this.$set(this.taskData, this.curIndex, obj);
      this.$nextTick(() => {});
      setTimeout(() => {
        this.$refs.scroll[this.curIndex].refresh();
      }, 500);
    },
    _getTaskData(flag) {
      if (!JSON.parse(localStorage.getItem("isUser"))) {
        this.taskData = [];
        this.$vux.toast.show({
          text: "游客暂无权限，请先注册会员",
        });
        return;
      }
      var params = {
        token: localStorage.getItem("token"),
      };
      if (flag) {
        this.$vux.loading.show();
      }
      this.$http
        .post(this.urlRequest + "?m=api&c=app&a=taskIndex", params)
        .then((res) => {
          if (flag) {
            this.$vux.loading.hide();
          } else {
            this.$refs.scroll[this.curIndex].forceUpdate(true);
          }

          if (res.status == 0) {
            let arr = [];
            let initialData = [];
            let vm = this;
            vm.taskData = res.data;
            if (res.data[0].data[0].betting_need_money) {
              //是否有任务限制
              initialData = res.data[0].data;
            }
            for (var i = 0; i < vm.taskData.length; i++) {
              for (var j = 0; j < vm.taskData[i].data.length; j++) {
                vm.taskData[i].data[j].seen = false;
              }
            }
            for (var j = 0; j < vm.taskData.length; j++) {
              var sort = vm.taskData[j];
              sort.data = vm.taskSort(sort.data);
              arr.push(sort);
            }
            if (initialData.length && initialData[0].betting_need_money) {
              arr[0].data = initialData;
            }
            vm.taskData = arr;
          }
        })
        .catch((error) => {
          this.$refs.scroll[this.curIndex].forceUpdate(false);
          this.$vux.loading.hide();
          console.log(error);
        });
    },
    taskSort(array) {
      var y = [],
        y1 = 0;
      for (var i = 0; i < array.length; i++) {
        if (array[i].task_state == 1) {
          y.splice(0, 0, array[i]);
          y1++;
        }
        if (array[i].task_state == 2) {
          y.splice(y1, 0, array[i]);
          y1++;
        }
        if (array[i].task_state == 3) {
          y.splice(y.length, 0, array[i]);
        }
      }
      return y;
    },
    getAward(id, index, money) {
      if (!id) {
        this.$vux.toast.show({
          text: "领取失败，请刷新后重试",
        });
        return;
      }
      var params = {
        token: localStorage.getItem("token"),
        id: id,
      };
      this.$vux.loading.show();
      this.$http
        .post(this.urlRequest + "?m=api&c=app&a=receiveTaskReward", params)
        .then((res) => {
          this.$vux.loading.hide();
          if (res.status == 0) {
            var that = this;
            this.$vux.alert.show({
              content:
                '<p style="color:#000">恭喜您获得<span class="text-red" style="margin:0 4px">' +
                money +
                '</span>元宝<i class="icon-wallet-gold" style="margin-left:4px"></i></p>',
              onHide() {
                var obj = {};
                obj = that.taskData[that.curIndex];
                obj.data[index].task_state = 3;
                obj.data = that.taskSort(obj.data);
                that.$set(that.taskData, that.curIndex, obj);
                that.taskSort(that.taskData[that.curIndex].data);
                var accountData = that.accountData;
                accountData.money_usable = Number(accountData.money_usable);
                accountData.money_usable += Number(money);
                that.SET_ACCOUNT_DATA({
                  Obj: accountData,
                });
              },
            });
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              this.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
        });
    },
    pullingDown() {
      this._getTaskData(0);
    },
    hasFinish(curIndex) {
      for (var j = 0; j < this.taskData.length; j++) {
        for (var i = 0; i < this.taskData[j].data.length; i++) {
          if (
            (this.taskData[j].data[i].task_state == 1 && j == curIndex) ||
            (this.taskData[j].data[i].id == 7 &&
              this.taskData[j].data[i].task_state == 2 &&
              j == curIndex)
          ) {
            return true;
          }
        }
      }
      return false;
    },
    goHome() {
      this.$router.push({
        path: "/home",
      });
    },
    signInClose() {
      //关闭签到弹窗
      this.signInFlag = false;
    },
    _getSignCont(index) {
      //获取签到次数接口
      this.signFlag = index;
      var params = {
        token: localStorage.getItem("token"),
      };
      this.$vux.loading.show();
      this.$http
        .post(this.urlRequest + "?m=api&c=app&a=getSignCont", params)
        .then((res) => {
          this.$vux.loading.hide();
          this.signStatus = res;
          if (res.status == -1) {
            //签到没有开启
            return;
          }
          if (res.status == 0) {
            if (res.data.is_sign == 1) {
              //已签到
              return;
            }
            var signObj = this.getSignObj;
            signObj.signCurrent = res.data.count;
            signObj.signState = res.data.is_sign;
            this.signMoneyBal = res.data.money_bal;
            this.signMoney = res.data.money;

            this.$store.commit("setSignObj", signObj);
            if (signObj.signState == 0) {
              var vm = this;
              setTimeout(function () {
                vm.signInFlag = true;
              }, 20);
            }
          }
        })
        .catch((error) => {
          this.$vux.loading.hide();
          console.log(error);
        });
    },
    taskSign(index) {
      //签到
      if (this.getSignObj.signState != 0) {
        return false;
      }
      if (index && index - this.getSignObj.signCurrent != 1) {
        return false;
      }
      var params = {
        token: localStorage.getItem("token"),
        type: 7,
      };
      this.$vux.loading.show();
      this.$http
        .post(this.urlRequest + "?m=api&c=app&a=taskSign", params)
        .then((res) => {
          this.$vux.loading.hide();
          if (res.status == 0) {
            var signObj = this.getSignObj;
            signObj.signState = 1;
            signObj.signCurrent += 1;
            this.$store.commit("setSignObj", signObj);
            var accountData = this.accountData;
            accountData.money_usable = Number(accountData.money_usable);
            accountData.money_usable += Number(
              this.getSignObj.signCurrent < 7
                ? this.signMoney
                : this.signMoneyBal
            );
            console.log(accountData.money_usable);
            this.SET_ACCOUNT_DATA({
              Obj: accountData,
            });
            this.signInOKFlag = true;

            var that = this;
            var obj = {};
            obj = that.taskData[that.curIndex];
            obj.data[this.signFlag].task_state = 3;
            obj.data = that.taskSort(obj.data);
            that.$set(that.taskData, that.curIndex, obj);
            that.taskSort(that.taskData[that.curIndex].data);

            var vm = this;
            setTimeout(function () {
              vm.signInFlag = false;
              vm.signInOKFlag = false;
            }, 3000);
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              this.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
        })
        .catch((error) => {
          this.$vux.loading.hide();
          console.log(error);
        });
    },
  },
  watch: {},
};

// WEBPACK FOOTER //
// ./src/components/personalCenter/taskCenter/taskCenter.vue
